%%% ------------------------------------------------------------------
%%% Licensed under the Apache License, Version 2.0 (the 'License');
%%%  you may not use this file except in compliance with the License.
%%%  You may obtain a copy of the License at
%%%
%%%      http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Copyright (c) 2017 qingchuwudi <bypf2009@vip.qq.com>
%%%
%%%  Unless required by applicable law or agreed to in writing, software
%%%  distributed under the License is distributed on an 'AS IS' BASIS,
%%%  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%%  See the License for the specific language governing permissions and
%%%  limitations under the License.
%%%
%%% @doc
%%% @author  qingchuwudi <'bypf2009@vip.qq.com'>
%%% @copyright 2017 qingchuwudi <bypf2009@vip.qq.com>
%%% @end
%%% created|changed : 2017-09-12 14:53
%%% coding : utf-8
%%% ------------------------------------------------------------------
-module(elcache_ts).
-author("qingchuwudi").

-behaviour(gen_server).
-export([
    start_link/0,
    send_msg/1
]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).

%% -----------------------------
%%    对外api
%% -----------------------------

start_link() ->
    gen_server:start_link(?MODULE, {}, []).

send_msg(Pid) ->
    gen_server:cast(Pid, send_msg).

%% -----------------------------
%%    回调函数
%% -----------------------------

%% 初始化
init({}) ->
    {ok, crypto:rand_bytes(100)}.

%% 同步
handle_call(_Request, _From, State) ->
    {reply, {error, unknown_call}, State}.

%% 异步
handle_cast(send_msg, State) ->
    Fun = fun
        F(0) ->
            ok;
        F(C) ->
            elcache:add(test, State),
            F(C - 1)
    end,
    Fun(10),
    {noreply, State};

handle_cast(_Msg, State) ->
    {noreply, State}.

%% 消息
handle_info(_Info, State) ->
    {noreply, State}.

%% 终止处理
terminate(_Reason, _State) ->
    ok.

%% 热更替换
code_change(_OldVsn, State, _Extra) ->
    {ok, State}.